package com.mingweicai.intergration.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyInvocationHandler implements InvocationHandler {

  private Object target;

  /**
   *
   * @param o 代理对象
   * @param method  代理方法
   * @param objects  方法参数列表
   * @return
   * @throws Throwable
   */
  @Override
  public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
    System.out.println("------经纪人谈判------");
    Object result = method.invoke(target,objects);
    System.out.println("------经纪人收费------");
    return result;
  }

  //定义获取代理对象方法
  public Object getJDKProxy(Object targetObject){
    //为目标对象target赋值
    this.target = targetObject;
    //JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance 函数所需参数就可看出
    return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this);
  }

}
